package view;


import java.io.Serializable;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import javax.swing.JButton;

import model.dtos.CartaDTO;
import model.dtos.LocalDTO;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;

import rmi.TDAManejoDatos;
//import controllers.AdministracionController;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PanelAltaCarta extends javax.swing.JPanel implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton jButton;
	private static PanelAltaCarta instancia;
	private JLabel jLabelAltaCta;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JTextField NombreTextField;
	private JLabel NombreLabel;
	private JButton jButtonGuardar;
	private JTextField jTextFieldFecHta;
	private JLabel jLabelFechHta;
	private JTextField jTextFieldFecDde;
	private JLabel jLabelFecDde;
	private JLabel jLabelSector;
	private JComboBox jComboBoxIDLocal;
	
	
	//RMI
	TDAManejoDatos manejoCartas;
	CartaDTO carta;
	
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	
	public static PanelAltaCarta getInstancia(){
		if(instancia==null){
			instancia=new PanelAltaCarta();
		}
		return instancia;
	}
	
	
	private PanelAltaCarta() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setSize(400, 219);
			this.setLayout(null);
			{
				jButton = new JButton();
				this.add(jButton);
				this.add(getJLabelSector());
				jButton.setText("Cancelar");
				jButton.setBounds(211, 189, 87, 21);
				jButton.setAction(getAppActionMap().get("actionCancelar"));
				this.setVisible(true);
				this.setPreferredSize(new java.awt.Dimension(400, 238));
			}
			{
//				ComboBoxModel jComboBoxIDLocalModel = 
//						new DefaultComboBoxModel(
//								new String[] { "Item One", "Item Two" });
//				jComboBoxIDLocal = new JComboBox();
//				this.add(jComboBoxIDLocal);
				this.add(getJLabelFecDde());
				this.add(getJTextFieldFecDde());
				this.add(getJLabelFechHta());
				this.add(getJTextFieldFecHta());
				this.add(getJLabelAltaCta());
				this.add(getJButtonGuardar());
//				jComboBoxIDLocal.setModel(jComboBoxIDLocalModel);
//				jComboBoxIDLocal.setBounds(212, 46, 94, 21);
				this.add(getjMozoIDLocal());
				this.add(getNombreLabel());
				this.add(getNombreTextField());
				this.add(getJLabel1());
				this.add(getJLabel2());
			}
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//RMI
		public boolean getStub() {
			System.setSecurityManager(new RMISecurityManager()); //RMI
			try {
				manejoCartas = (TDAManejoDatos)Naming.lookup ("//localhost/Gestiones");
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
	    }
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private JComboBox getjMozoIDLocal() 
	{
		if(getStub())
		{		
			try {
					jComboBoxIDLocal = new JComboBox();
					Collection<LocalDTO> _locales;
					
						_locales = manejoCartas.getLocales();
					
					for (LocalDTO _local : _locales)
					{
						jComboBoxIDLocal.addItem(_local.getNombre());
					}
					jComboBoxIDLocal.setBounds(212, 46, 94, 21);
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
		}
		return jComboBoxIDLocal;
	}
	
	
	@Action
	public void actionCancelar() {
		instancia.setVisible(false);
	}
	
    /**
    * Returns the action map used by this application.
     * Actions defined using the Action annotation
     * are returned by this method
     */
	private ApplicationActionMap getAppActionMap() {
		return Application.getInstance().getContext().getActionMap(this);
	}
    
    private JLabel getJLabelSector() {
    	if(jLabelSector == null) {
    		jLabelSector = new JLabel();
    		jLabelSector.setBounds(98, 49, 77, 14);
    		jLabelSector.setText("ID de Sector:");
    	}
    	return jLabelSector;
    }
    
    private JLabel getJLabelFecDde() {
    	if(jLabelFecDde == null) {
    		jLabelFecDde = new JLabel();
    		jLabelFecDde.setBounds(98, 116, 101, 14);
    		jLabelFecDde.setText("Fecha Desde:");
    		jLabelFecDde.setVisible(true);
    	}
    	return jLabelFecDde;
    }
    
    private JTextField getJTextFieldFecDde() {
    	if(jTextFieldFecDde == null) {
    		jTextFieldFecDde = new JTextField();
    		jTextFieldFecDde.setBounds(212, 116, 94, 21);
    		
    	}
    	return jTextFieldFecDde;
    }
    
    private JLabel getJLabelFechHta() {
    	if(jLabelFechHta == null) {
    		jLabelFechHta = new JLabel();
    		jLabelFechHta.setBounds(98, 150, 101, 14);
    		jLabelFechHta.setText("Fecha Hasta:");
    		jLabelFechHta.setVisible(true);
    	}
    	return jLabelFechHta;
    }
    
    private JTextField getJTextFieldFecHta() {
    	if(jTextFieldFecHta == null) {
    		jTextFieldFecHta = new JTextField();
    		jTextFieldFecHta.setBounds(212, 150, 94, 21);
    	}
    	return jTextFieldFecHta;
    }
    
    private JLabel getJLabelAltaCta() {
    	if(jLabelAltaCta == null) {
    		jLabelAltaCta = new JLabel();
    		jLabelAltaCta.setBounds(148, 20, 96, 14);
    		jLabelAltaCta.setText("Alta de nueva Carta:");
    	}
    	return jLabelAltaCta;
    }
    
    private JButton getJButtonGuardar() {
    	if(jButtonGuardar == null) {
    		jButtonGuardar = new JButton();
    		jButtonGuardar.setBounds(109, 189, 91, 21);
    		jButtonGuardar.setText("Guardar");
    		jButtonGuardar.setAction(getAppActionMap().get("ActionGuardar"));
    	}
    	return jButtonGuardar;
    }
    
	@Action
	public void ActionGuardar()
	
	{
			try
			{
				CartaDTO nuevaCarta = new CartaDTO();
				
				Collection<LocalDTO> _locales = manejoCartas.getLocales();
				for (LocalDTO _local : _locales)
				{
					if (_local.getNombre().equals(jComboBoxIDLocal.getSelectedItem().toString()))
					{
						nuevaCarta.setIdLocal(_local.getId());
					}
				}
				
				DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
				try {
					Date auxFechaDesde = dateFormat.parse(jTextFieldFecDde.getText());
					nuevaCarta.setFechaDesde(auxFechaDesde);
					
					Date auxFechaHasta = dateFormat.parse(jTextFieldFecHta.getText());
					nuevaCarta.setFechaHasta( auxFechaHasta);
					
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				nuevaCarta.setNombre(NombreTextField.getText());
				manejoCartas.guardarCarta(nuevaCarta);
				//AdministracionController.getInstancia().guardarCarta(nuevaCarta);
			} catch (RemoteException e) 
			{
    			// TODO Auto-generated catch block
    			e.printStackTrace();
			}
		}
	
	private JLabel getNombreLabel() {
		if(NombreLabel == null) {
			NombreLabel = new JLabel();
			NombreLabel.setBounds(98, 82, 83, 16);
			NombreLabel.setText("Nombre:");
		}
		return NombreLabel;
	}
	
	private JTextField getNombreTextField() {
		if(NombreTextField == null) {
			NombreTextField = new JTextField();
			NombreTextField.setBounds(211, 82, 95, 21);
		}
		return NombreTextField;
	}
	
	private JLabel getJLabel1() {
		if(jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(307, 152, 93, 16);
			jLabel1.setText("dd/MM/aaaa");
		}
		return jLabel1;
	}
	
	private JLabel getJLabel2() {
		if(jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("dd/MM/aaaa");
			jLabel2.setBounds(307, 121, 93, 16);
		}
		return jLabel2;
	}

}
